package com.install.core.util;

import java.util.ArrayList;
import java.util.List;

public class TreeNodeBulid {

	public static List buildTree(String parentNodeID, List data) {
		List list = new ArrayList();
		try{
			if ((data == null) || (data.size() == 0)) {
				return data;
			}
			
			for (int i = 0; i < data.size();i++) {
				SimpleTreeNode node = (SimpleTreeNode) data.get(i);
				if(parentNodeID.equals(node.getPid())){
					list.add(node);
					data.remove(node);
					addNode(node, data); 
				}
			}
			return list;
		}catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		
	}

	private static void addNode(SimpleTreeNode pNode, List data) {
		for (int i = 0; i < data.size(); ++i) {
			SimpleTreeNode node = (SimpleTreeNode) data.get(i);
			if (node.isChildren(pNode)) {
				pNode.addChildren(node);
				if (!node.isLeaf())
					addNode(node, data);
			}
		}
	}

}
